// pages/personal/address/address.js
let app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    addressList: [],   //所有收货地址
    backgroundImg: 'http://yanxuan.nosdn.127.net/hxm/yanxuan-wap/p/20161201/style/img/icon-normal/noAddress-26d570cefa.png',
    addressid: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getAddressList();//页面加载自动执行后去地址列表数据的方法
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  //获取收货地址列表
  getAddressList() {
    let that = this;
    wx.request({
      url: app.globalData.api.AddressList,
      header: {
        "content-type": "application/json",
        'X-Nideshop-Token': wx.getStorageSync('token')
      },
      success: (res) => {
        that.setData({
          addressList: res.data.data//拿到的地址列表
        });
        // console.log("拿到的地址列表",this.data.addressList)
      }

    })

  },


  editAdd(e) {
    let addid = e.currentTarget.dataset['addid'];
    console.log(addid);
    wx.redirectTo({
      url: '../addAddress/addAddress?id=' + addid,
    })
  },

  //DeleteAddress删除地址
  deleteAdd(e) {
    console.log(e.target.dataset.addressid);
    let addid = e.target.dataset.addressid;
    let that = this;
    wx.showModal({
      title: '',
      content: '确定要删除地址？',
      success: function (res) {
        if (res.confirm) {
          // util.request(api.AddressDelete, { id: addressId }, 'POST').then(function (res) {
          //   if (res.errno === 0) {
          //     that.getAddressList();
          //   }
          // });
          // console.log('用户点击确定')
          wx.request({
            url: app.globalData.api.DeleteAddress,
            data: {
              id: addid
            },
            method: 'POST',
            header: {
              "content-type": "application/json",
              'X-Nideshop-Token': wx.getStorageSync('token')
            },
            success: (res) => {
              console.log(res)
              if (res.data.errno === 0) {
                wx.showToast({
                  title: '地址已删除',
                  icon: 'none'
                })
                that.getAddressList();
              }
            }
          })
        }
      }
    })
    return false;

  }
})